package com.sptpc.stu.controller.router;

import com.sptpc.stu.common.utils.LocalDateUtil;
import com.sptpc.stu.model.Course;
import com.sptpc.stu.model.Student;
import com.sptpc.stu.repository.CourseRepository;
import com.sptpc.stu.repository.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;

/**
 * ManageRouterController
 * Description: 管理后台的路由控制器
 * @author alexchen
 */
@Controller
public class ManageRouterController {

    @Autowired
    private StudentRepository studentRepository;


    @Autowired
    private CourseRepository courseRepository;


    // -----------------------------------------------
    // 管理后台登录和首页路由
    // -----------------------------------------------
    @GetMapping("/")
    public String manageDefault() {
        return "manage/login";
    }

    @GetMapping("manage/login")
    public String manageLogin() {
        return "manage/login";
    }

    @GetMapping("manage/index")
    public String manageIndex() {
        return "manage/index";
    }

    @GetMapping("manage/welcome")
    public String welcome() {
        return "manage/welcome";
    }

    // -----------------------------------------------
    // 学生管理的路由
    // -----------------------------------------------
    @GetMapping("stu/index")
    public String studentIndex() {
        return "student/index";
    }

    @GetMapping("stu/form")
    public String studentForm() {
        return "student/form";
    }

    @GetMapping("stu/edit/{id}")
    public ModelAndView studentEditForm(@PathVariable Long id) {
        ModelAndView mv = new ModelAndView("student/edit");
        Student stu = studentRepository.findById(id).get();
        mv.addObject("data", stu);
        mv.addObject("enrollDate", LocalDateUtil.localDate2Date(stu.getEnrollDate()));
        return mv;
    }

    @GetMapping("cur/index")
    public String courseIndex() {
        return "course/index";
    }

    @GetMapping("cur/edit/{id}")
    public ModelAndView courseEdit(@PathVariable Long id) {
        ModelAndView mv = new ModelAndView("course/edit");
        Course course = courseRepository.findById(id).get();
        mv.addObject("data",course);
        return mv;
    }

    @GetMapping("cur/form")
    public String courseForm() {
        return "course/form";
    }



}
